from pymem import Pymem
from pymem.exception import MemoryReadError, WinAPIError, ProcessNotFound
from os import path
from time import sleep
from sys import exit

def readPointer(ModuleName, offsets):
    def get_module_addr(ModuleName):
        modules = list(process.list_modules())
        for module in modules:
            if module.name == ModuleName:
                return module.lpBaseOfDll
        return None
    base_module = get_module_addr(ModuleName)
    if base_module is not None:
        try:
            current_address = base_module
            for i, offset in enumerate(offsets):
                if i < len(offsets) - 1:
                    current_address = process.read_int(current_address + offset)                    
                else:
                    if current_address == 0:
                        #print('当前地址为零')
                        return None
                    current_address += offset
                if current_address == 0:
                 #print('当前地址为零')
                 return None
            return current_address
        except MemoryReadError as e:
            #print(f'无法读取内存: {e}')
            return None
        except WinAPIError as e:
            #print(f'Windows API错误: {e}')
            return None
    else:
        #print('找不到模块基址')
        return None

def ConnectProcess(ProcessName):
    while True:
        try:
            process = Pymem(ProcessName)
            print(f"已找到 {ProcessName}")
            return process
        except ProcessNotFound:
            print("进程未找到，5秒后重试...")
            sleep(5)

def PlayActionVoice(ModuleName, offsets, ActionValueCache):
    try:
        Address = readPointer(ModuleName, offsets)
        if Address:
            try:
                RoleNumber = process.read_int(Address + 528)
                ActionValue = process.read_int(Address)
                #ChangeStatus = process.read_int(Address + 8)
                voice = f"{RoleNumber}\\{RoleNumber}_{ActionValue}.wav"
                if path.isfile(voice) and ActionValue != ActionValueCache:
                    print(voice)
                    sound = Sound(voice)
                    sound.play()

                return ActionValue
            except MemoryReadError as e:
                #print(f"偏移处读取内存失败: {e}")
                return
    except Exception as e:
        #print(f"出现错误: {e}")
        return

def PlayEntranceVoice(ModuleName, P1Pointer, P2Pointer, TimePointer):
        try:
            P1Address = readPointer(ModuleName, P1Pointer)
            P2Address = readPointer(ModuleName, P2Pointer)
            TimeAddress = readPointer(ModuleName, TimePointer)
            if P1Address and P2Address:
                P1RoleNumber = process.read_int(P1Address + 528)
                P2RoleNumber = process.read_int(P2Address + 528)
                TimeValue = process.read_int(TimeAddress)
                voice = f"{P1RoleNumber}\\{P1RoleNumber}-{P2RoleNumber}.wav"
                if not path.isfile(voice):
                    voice = f"{P1RoleNumber}\\{P1RoleNumber}-.wav"
                if 0 < TimeValue < 10:
                    music.load(voice)
                    music.play()
                    print(voice)
        except MemoryReadError as e:
            #print(f"读取内存失败: {e}")
            return
        except Exception as e:
            #print(f"出现错误: {e}")
            return

if not path.isfile('0/0-0.wav'):
    print("请把本程序与语音文件放置同一目录")
    input("按任意键退出...")
    exit()


ProcessName = "th123.exe"
ModuleName = "th123.exe"
P1Pointer = [0x49864C, 0x4C]
P2Pointer = [0x498650, 0x4C]
TimePointer = [0x60D84,0x0]


process = ConnectProcess(ProcessName)
from pygame.mixer import Sound, music, init
init()

P1ActionValueCache = P2ActionValueCache = 0
while True:
  PlayEntranceVoice(ModuleName, P1Pointer, P2Pointer, TimePointer)
  P1ActionValueCache = PlayActionVoice(ModuleName, P1Pointer, P1ActionValueCache)
  P2ActionValueCache = PlayActionVoice(ModuleName, P2Pointer, P2ActionValueCache)

process.close_process()